//
//  BCCircleView.m
//  BangcaiBuy
//
//  Created by lsy on 16/7/6.
//  Copyright © 2016年 BCB. All rights reserved.
//

#import "BCCircleView.h"
#define angle2Arc(angle) (angle * M_PI /180)

@interface BCCircleView ()
/**
 *  弧度
 */
@property (nonatomic,assign)CGFloat angle;

/**
 *  圆心
 */
@property (nonatomic,assign)CGPoint circleCenter;

/**
 *  定时器变量
 */
@property (nonatomic,assign)CGFloat value;

@end

@implementation BCCircleView

-(instancetype)init{
    if (self = [super init]) {
        [self defaultColor];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self defaultColor];
    }
    return self;
}

-(instancetype)initWithLineColor:(UIColor*)lineColor{
    if (self = [super init]) {
        self.lineColor = lineColor;
    }
    return self;
}

-(void)defaultColor{
    _lineColor = [UIColor orangeColor];
}


- (void)drawRect:(CGRect)rect {
    
    _circleCenter = CGPointMake(rect.size.width /2 , rect.size.height/2);
    
    CGFloat radius = MIN(rect.size.height, rect.size.width) * 0.5;
    
    UIBezierPath* path = [UIBezierPath
                          bezierPathWithArcCenter:_circleCenter
                          radius:radius*0.765
                          startAngle:M_PI_4 + M_PI_2
                          endAngle:M_PI_4 + M_PI_2+ angle2Arc(self.angle)
                          clockwise:YES];
    [self.lineColor set];
    [path setLineCapStyle:kCGLineCapRound];
    path.lineWidth = radius* 0.22;
    [path stroke];
    
    NSLog(@"%.2f", self.angle);
}



-(void)setPercent:(CGFloat)precent{
    if (precent>100) {
        precent = 100;
    }
    _percent = precent;
    
    _angle =  _percent /100 * 270;
    [self setNeedsDisplay];
}



@end
